<?php
namespace app\wechat\controller;
use app\api\service\MemberWithdraw;
use think\Loader;
use think\Db;
use Jssdk\JSSDK;

class WDistribution extends Common
{
    // 分销首页
    public function index()
    {
        $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
        $signPackage = $jssdk->getSignPackage();
        $this->assign('signPackage', $signPackage);

        $member_id = session('member_id');
        $member = db('member')->field('wx_qr_code, phone, distribution_money, distribution_money_frozen, distribution_money_all, id')->where('id', $member_id)->find();
        $erweima_text = 'http://' . $_SERVER['HTTP_HOST'] . url('register', ['phone' => $member['phone'], 'action' => 'distribution_', 'id' => $member['id']]);
        if (!$member['wx_qr_code']) {
            // 二维码不存在  生成二维码
            Loader::import('QRcode.phpqrcode');

            $erweima_image = 'distribution' . DS . time() . '_' . $member['phone'] . '.png';  // 出库二维码地址
            $erweima_url = config('new.upload_img_save_dir') . DS .$erweima_image;
            if (!is_dir(config('new.upload_img_save_dir') . DS . 'distribution')) {
                mkdir(config('new.upload_img_save_dir') . DS . 'distribution', 0777, true);
            }
            \QRcode::png($erweima_text, $erweima_url, '', 6, 2); //生成二维码
            erweima_add_log_($erweima_url); //添加logo
            db('member')->where('id', $member_id)->update(['wx_qr_code' => '/uploads/' . $erweima_image]);
            $member['wx_qr_code'] = $erweima_image;
        }

        // 获取下级用户
        $one_num = $two_num = 0;
        $one_list = db('member')->where('parent_id', $member_id)->column('id');
        if ($one_list) {
            $one_num = count($one_list);
            $two_num = db('member')->where('parent_id', 'in', $one_list)->count();
        }
        $member['one_num'] = $one_num;
        $member['two_num'] = $two_num;
        $this->assign('member', $member);
        $this->assign('erweima_text', $erweima_text);
        
        $image = 'http://' . $_SERVER['HTTP_HOST'] . '/static/wechat/images/images/images/deng1_03.jpg';
        $this->assign('image', $image);

        $type = input('param.type');
        $this->assign('type', $type);
        return view();
    }

    // 邀请注册页面
    public function register() {
        $openid = session('openid');
        $member = db('member')->where('openid', $openid)->find();
        if ($member) {
            $this->redirect(url('member/index'));
        }

        $phone = input('param.phone');
        $member_role_info = db('member_role_info')->where('member_id', input('param.id'))->find();
        $this->assign('phone', $phone);

        //dump($member_role_info);
        $this->assign('member_role_info', $member_role_info);
        return view();
    }

    // 分销金额兑现到可用余额
    public function distribution_of_cash_money() {
        $param = [
            'member_id' => session('member_id'),
            'type' => input('param.type')
        ];
        $data = call_interface('distribution_of_cash_money',$param);
        //dump($data);
        return json($data);
    }
}
